﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    using DAL.Entities;

    public class ChiSoDienKeDAL : BaseDAL, UTL.IBaseDAL
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            string sql = @" SELECT *,b.diengiai as ten,(Case when TrangThai = 0 then N'Chưa ghi hoá đơn' else N'Đã ghi hoá đơn' end) as tinhtrang from chisodienke a join dienke b on a.ma_dk=b.ma_dk";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            throw new NotImplementedException();
        }

        public bool Insert(object obj)
        {
            var o = (Chi_So_Dien_Ke)obj;
            string sql = @"set dateformat mdy; INSERT INTO chisodienke(ma_dk,ngayghi,chisocu,chisomoi,trangthai) VALUES('{0}','{1}',{2},{3},'False')";
            sql = string.Format(sql,o.madk,o.ngayghi,o.chisocu,o.chisomoi);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Chi_So_Dien_Ke)obj;
            string sql = @"set dateformat mdy; PDATE chisodienke SET  chisocu={0}, chisomoi={1} WHERE madk='{2}' and ngayghi='{3}' ";
            sql = string.Format(sql,o.chisocu,o.chisomoi,o.madk,o.ngayghi);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool DeleteOBJ(object obj)
        {
            var o = (Chi_So_Dien_Ke)obj;
            string sql = @"delete from chisodienke where ma_dk='{0}' and ngayghi='{1}'";
            sql = string.Format(sql,o.madk, o.ngayghi);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        
        public DataTable Select_dienke()
        {
            string sql = @" SELECT ma_dk as id, diengiai as ten FROM dienke ";
            return ExecuteQuery(sql);
        }

        public string select_chisocu(string id)
        {
            string kq="0";

            string sql = @"select max(chisomoi) from chisodienke where ma_dk='{0}'";
            sql = string.Format(sql,id);
            DataTable dt = ExecuteQuery(sql);
            if(dt.Rows.Count>0)
                kq=dt.Rows[0][0]+"";
            return kq;
        }
    }
}
